package rbac

import (
	"github.com/gin-gonic/gin"
	"store/libs/helper"
	"store/libs/log"
	"store/models"
	"store/models/base"
)

const (
	TABLE_ADMIN_ROLE = "admin_role"
)

type AdminRole struct {
	IsOn       uint8  `json:"is_on" xml:"is_on"`
	AddTime    string `json:"add_time" xml:"add_time"`
	UpdateTime uint64 `json:"update_time" xml:"update_time"`
	Version    uint64 `json:"version" xml:"version"`
	Id         uint64 `json:"id" xml:"id"`
	RoleName   string `json:"role_name" xml:"role_name"`
}

/**
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRoleMap2Model(data map[string]string) AdminRole {
	return AdminRole{
		Id:         uint64(helper.ToInt(data["id"])),
		RoleName:   data["role_name"],
		IsOn:       uint8(helper.ToInt(data["is_on"])),
		AddTime:    data["add_time"],
		UpdateTime: uint64(helper.ToInt(data["update_time"])),
		Version:    uint64(helper.ToInt(data["version"])),
	}
}

/**
 * 获取所有启用的角色
 */
func AllEnableRoles(ctx *gin.Context) []AdminRole {
	roleList := []AdminRole{}

	rows, err := models.Orm.Table(TABLE_ADMIN_ROLE).Where("is_on = 1").Rows()
	if err != nil {
		log.Error(ctx, err.Error())
		return roleList
	}

	list := base.Rows2MapList(ctx, rows)
	if len(list) < 1 {
		return roleList
	}

	for _, m := range list {
		roleList = append(roleList, AdminRoleMap2Model(m))
	}
	return roleList
}

/**restful新增数据
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRoleCreate(ctx *gin.Context, info map[string]interface{}) uint64 {
	id := base.NewCommonModel(ctx).Create(TABLE_ADMIN_ROLE, info)
	return uint64(id)
}

/**restful删除，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRoleDelete(ctx *gin.Context, id uint64) int64 {
	return base.NewCommonModel(ctx).Delete(TABLE_ADMIN_ROLE, helper.ToString(id))
}

/**restful更新(带乐观锁)，数据库表中必须有id,version字段
uint64uint64uint64 * @email jhq0113@163.com
 * generated by gii
*/
func AdminRoleUpdate(ctx *gin.Context, id uint64, version uint64, set map[string]interface{}) int64 {
	return base.NewCommonModel(ctx).UpdateWithLock(TABLE_ADMIN_ROLE, helper.ToString(id), version, set)
}

/**restful禁用启用，数据库表中必须有is_on字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRoleUpOrDown(ctx *gin.Context, id uint64, value uint8) int64 {
	return base.NewCommonModel(ctx).UpOrDown(TABLE_ADMIN_ROLE, helper.ToString(id), value)
}

/**restful详情，数据库表中必须有id字段
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRoleInfo(ctx *gin.Context, where map[string]interface{}) AdminRole {
	info := base.NewCommonModel(ctx).Info(TABLE_ADMIN_ROLE, where)
	if info != nil {
		return AdminRoleMap2Model(info)
	}
	return AdminRole{}
}

/**restful列表分页检索
 * @email jhq0113@163.com
 * generated by gii
 */
func AdminRoleIndex(ctx *gin.Context, page int64, pageSize int64, where map[string]interface{}, orders []string) map[string]interface{} {
	result := base.NewCommonModel(ctx).Index(TABLE_ADMIN_ROLE, page, pageSize, where, orders)

	if len(result["list"].([]map[string]string)) > 0 {
		list := []AdminRole{}
		for _, data := range result["list"].([]map[string]string) {
			list = append(list, AdminRoleMap2Model(data))
		}
		result["list"] = list
	}

	return result
}
